package com.gzz.orchid.common.boot.mybatis;

import com.gzz.orchid.common.boot.mybatis.interceptor.PaginationInterceptor;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.logging.slf4j.Slf4jImpl;
import org.apache.ibatis.reflection.ReflectorFactory;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.logging.log4j.util.Strings;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * MyBatis - Customizer
 * @author higmd
 */
@Configuration
public class MyBatisConfiguraction {
    /**
     * 配置分页插件
     * @return
     */
    public PaginationInterceptor paginationInterceptor() {
       var interceptor =  new PaginationInterceptor();
        Properties prop = new Properties();
        prop.setProperty("DIALECT", "mysql");
        prop.setProperty("REGEX_SPLIT_PAGE", ".*ByPage$");
        interceptor.setProperties(prop);
        return  interceptor;
    }



    @Primary
    @Bean(name = "primaryDataSource")
    public DataSource dataSource() {
        HikariConfig config = new HikariConfig();
        //config.setPoolName("HikariCP 连接池");
        //config.setDataSourceClassName("oracle.jdbc.pool.OracleDataSource");
        config.setDriverClassName("com.mysql.cj.jdbc.Driver");
        config.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/orchid-db?useUnicode=true&characterEncoding=utf-8&nullCatalogMeansCurrent=true");
        config.setUsername("root");
        config.setPassword("passwd");
        // 使用HikariCP连接池
        config.setDataSourceClassName("com.zaxxer.hikari.HikariDataSource");
        config.setConnectionTimeout(60000L);
        config.setMaxLifetime(60000L);
        config.setMinimumIdle(5);
        config.setMaximumPoolSize(10);
        config.setValidationTimeout(3000L);
        config.setConnectionTestQuery("SELECT 1");
        //
        return new HikariDataSource(config);
    }

    @Bean
    public DataSourceTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }
    @Bean
    @ConditionalOnMissingBean
    public SqlSessionFactory sqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        //factoryBean.setVfs(SppringBootVFS.class);
        factoryBean.setPlugins(paginationInterceptor());

        // mapper 文件位置
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath*:mapper/**/*.xml"));
        // 配置
        var conf = new org.apache.ibatis.session.Configuration();
        conf.setMapUnderscoreToCamelCase(true);
        conf.setCacheEnabled(true);
        conf.setUseGeneratedKeys(true);
        conf.setDefaultStatementTimeout(30);
        conf.setDefaultExecutorType(ExecutorType.REUSE);
        conf.setLogImpl(Slf4jImpl.class);
        factoryBean.setConfiguration(conf);
        //
        return factoryBean.getObject();
    }

    @Bean
    @ConditionalOnMissingBean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory factory) {
        return new SqlSessionTemplate(factory);
    }


}
